package com.lanlengran.imagemake;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.Log;

/**
 * Created by 13924 on 2017/10/8.
 */

public class ImageUtils {
    static Bitmap bitmap2BlackImage(Bitmap bitmap){
        Bitmap blackImage=null;
        int[] pixels=new int[bitmap.getWidth()*bitmap.getHeight()];
       int[] pixels2=new int[pixels.length];
        bitmap.getPixels(pixels,0,bitmap.getWidth(),0,0,bitmap.getWidth(),bitmap.getHeight());
        int avg=0;
        long allValue=0;
        int allValueSize=0;
        for (int i=0;i<bitmap.getHeight();i++){
            long tmpAllValue=0;
            int valueSize=0;
            for (int j=0;j<bitmap.getWidth();j++){
                int p=pixels[i*bitmap.getWidth()+j];
                int red=p/256/256%256;
                int green=p/256%256;
                int blue=p%256;
                int newColor=(red+green+blue)/3;
                if (newColor<-50&&newColor>-230){
                    tmpAllValue=tmpAllValue+newColor;
                    valueSize++;
                }

            }
            if (valueSize!=0){
                allValueSize++;
                allValue=allValue+tmpAllValue/valueSize;
            }

//            Log.d("qin","tmpAllValue=="+tmpAllValue);
//            Log.d("qin","allValue=="+allValue);
        }
        avg= (int) ((int) (allValue/allValueSize)*1);
        Log.d("qin","avg=="+avg);
        for (int i=0;i<pixels.length;i++){
            int pixel=pixels[i];
            int red=pixel/256/256%256;
            int green=pixel/256%256;
            int blue=pixel%256;
            int newColor=(red+green+blue)/3;
        //    Log.d("qin","newColor"+newColor);
            if (newColor>avg){
                pixel=255*256*256+255*256+255;
            }else {
                pixel=0;
            }

            pixels2[i]=pixel;
        }
        blackImage=Bitmap.createBitmap(pixels2,bitmap.getWidth(),bitmap.getHeight(), Bitmap.Config.RGB_565);
        return blackImage;
    }
    public static void bitmapToTxt(Bitmap bitmap,String filePath){
        float size= (float) 0.1;
        boolean isLeft2Right=true;
        StringBuffer stringBuffer=new StringBuffer();
        for (int y=0;y<bitmap.getHeight();y=y+3){
            int lastColor= Color.WHITE;
            int lastpos= 0;

            stringBuffer.append("G0 Y"+y*size+"\n");
            for (int index=0;index<bitmap.getWidth();index++){
                int x=0;
                if (isLeft2Right){
                    x=index;
                }else {
                    x=bitmap.getWidth()-index-1;
                }
                Log.d("qin","x==="+x);
                int nowColor=bitmap.getPixel(x,y);
                if (lastColor!=nowColor){

                    if (lastColor==Color.WHITE){
                        if ((index-lastpos)>10){
                            stringBuffer.append("\nM05\n");
                        }
                        stringBuffer.append("G0 X"+(bitmap.getWidth()-x)*size+"\n");
                    }else {
                        stringBuffer.append("\nM03\n");
                        stringBuffer.append("G1 X"+(bitmap.getWidth()-x)*size+"\n");
                    }
                    lastColor=nowColor;
                    lastpos=index;
                }

            }
//            if (isLeft2Right){
//                stringBuffer.append("G0 X"+bitmap.getWidth()*size+"\n");
//            }else {
//                stringBuffer.append("G0 X0 \n");
//            }
            isLeft2Right=!isLeft2Right;

        }
        AboutFileUtil.saveSettingFile(filePath,";Export By diy8000.taobao.com\n" +
                "S1000\n" +
                "G0 F90\n" +
                "G92 X0 Y0 Z0\n" +
                "G21\n" +
                "G90\n" +
                "G0 F90\n" +
                "G0 X0 Y0\n" +
                "M03 \n"+stringBuffer.toString()+"\nM05\n" +
                "G0 X0 Y0");
    }
}
